package Client;

import SQL.Connections;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;

public class Signup extends JFrame{
    private static JFrame frameglobal;
    private JFrame frame;
    private JTextField flnameField;
    private JTextField idField;
    private JTextField phoneField;
    private JTextField usernameField;
    private JButton 注册Button;
    private JPanel panel1;
    private JPasswordField passwordField1;//密码区域
    private JPasswordField passwordField2;
    private JLabel usernameLength;//用户名长度的标签
    private JLabel passwordsLength;//密码长度标签
    private JLabel title;
    private JButton 返回Buttom;


    private Connection con;
    private Statement stm;
    private ResultSet rs;
    private Connections connections = new Connections();


    public Signup() {
        注册Button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String name = flnameField.getText();
                String id = idField.getText();
                String phone = phoneField.getText();
                String username = usernameField.getText();
                String pw = passwordField1.getText();
                String pw2 = passwordField2.getText();

                signup(name,id,phone,username,pw,pw2);
            }
        });

        返回Buttom.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                Login.initialize();
                frameglobal.dispose();
            }
        });

        //添加键盘监听事件 按enter可以直接注册
        passwordField2.addKeyListener(new KeyAdapter() {
            @Override
            public void keyTyped(KeyEvent e) {
                super.keyTyped(e);
                if (e.getKeyChar() == '\n') {
                    String name = flnameField.getText();
                    String id = idField.getText();
                    String phone = phoneField.getText();
                    String username = usernameField.getText();
                    String pw = passwordField1.getText();
                    String pw2 = passwordField2.getText();

                    signup(name,id,phone,username,pw,pw2);
                }
            }
        });

    }

    private void signup(String name, String id, String phone, String username, String pw, String pw2) {
        try{
            Connection con = connections.getConnection();
            stm = con.createStatement();

            //判断注册信息是否规范
            if(username.length()>=6 && username.length()<=15 && pw.equals(pw2) && pw.length()>=6 && pw.length()<=15){

                int passengerType = 0;//0表示旅客
                //插入表
                int result = stm.executeUpdate("INSERT INTO usertrain(NameOfUser,IDCardNumber,UserPhoneNumber,UserName,UserPassword,UserType) VALUES('"+name+"','"+id+"','"+phone+"','"+username+"','"+pw+"','"+passengerType+"')");


                if(result==1){
                    JOptionPane.showMessageDialog(panel1,"注册成功");
                }


            }
            else if(username.length()<6){
                usernameLength.setText("用户名长度不够");
            }else if(username.length()>15){
                usernameLength.setText("用户名长度不能超过15位");
            }else if(pw.length()<6){
                passwordsLength.setText("密码长度不够");
            }else if(pw.length()>15){
                passwordsLength.setText("密码长度不能超过15位");
            }else if(!(pw.equals(pw2))){
                passwordsLength.setText("两次输入的密码不一致");
            }

            stm.close();
            con.close();
        }catch (Exception e){
            e.printStackTrace();
            JOptionPane.showMessageDialog(panel1,"您已经注册过");
        }

    }

    public static void main(String[] args) {
        initialize();


    }

    public static void initialize() {//创建并初始化注册界面
        JFrame frame = new JFrame("Signup");
        frame.setContentPane(new Signup().panel1);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setLocationRelativeTo(null);//设置屏幕居中
        frame.setVisible(true);
        frameglobal=frame;
    }


    private void createUIComponents() {
        // TODO: place custom component creation code here
        title = new JLabel(new ImageIcon("sign-up.png"));
    }
}
